//
// Created by 86150 on 2021/11/16.
//

#include "client_app.h"
#include <QtNetwork/QTcpSocket>

void client_app::open_camera()
{
	on = 1;

	cap.open(0);//camera perpare
	cap.set(cv::CAP_PROP_FRAME_WIDTH, 448);
	cap.set(cv::CAP_PROP_FRAME_HEIGHT, 448);
}
void client_app::see()
{
	if (!on) return;
	cap>>frame;

	QImage qbg = QImage((const unsigned char*)(frame.data),
						frame.cols,
						frame.rows,
						QImage::Format_BGR888
						);
	ui.label->setPixmap(QPixmap::fromImage(qbg.scaled(ui.label->size(), Qt::KeepAspectRatio)));
}
void client_app::shut_camera()
{
	on = 0;
	cap.release();
}
client_app::client_app(): cap(0), on(false)
{
	ui.setupUi(&ui);
	timer.start(10);

	QObject::connect(ui.openB, &QPushButton::clicked, this, &client_app::open_camera);
	QObject::connect(ui.shutB, &QPushButton::clicked, this, &client_app::shut_camera);
	QObject::connect(&timer, &QTimer::timeout, this, &client_app::see);
	QObject::connect(ui.recB, &QPushButton::clicked, this, &client_app::detect);
	QObject::connect(ui.goB, &QPushButton::clicked, this, &client_app::send_msg);
	QObject::connect(&chater, &QTcpSocket::readyRead, this, &client_app::refresh_msg);
	QObject::connect(ui.rgB, &QPushButton::clicked, this, &client_app::rgst);
	QObject::connect(ui.rgB_2, &QPushButton::clicked, this, &client_app::out);
	QObject::connect(ui.postB, &QPushButton::clicked, this, &client_app::post);

	QMetaObject::connectSlotsByName(this);
}
void client_app::detect()
{
	int idx = AlexNet.detect(frame);
	outcome = AlexNet.list4kind[idx];
	outcome4posting = AlexNet.list2put[idx];
	ui.show_out->clear();
	ui.show_out->setText(QString::fromStdString(outcome));
}
void client_app::Show()
{
	ui.show();
}
void client_app::send_msg()
{
	ui.msg->text().length();
	chater.write((ui.usr->text().toStdString() + ">>" + ui.msg->text().toStdString()).c_str(), 100);
	ui.msg->clear();
}
void client_app::refresh_msg()
{
	char buffer[100];

	chater.read(buffer, 100);
	ui.textBrowser->append(QString(buffer));
}
void client_app::rgst()
{
	if (ui.ip->text().length() == 0 or ui.port->text().length() == 0) {
		QMessageBox box;
		box.setText("please enter your name or ip or port!");
		box.exec();
	}

	QString ip = ui.ip->text();
	qint16 port = ui.port->text().toInt();

	chater.connectToHost(ip, port);
	if (chater.state() != QAbstractSocket::ConnectedState) {
		QMessageBox box;
		box.setText("connected fail");
		box.exec();
	}

	std::string cfg("<chat>");
	chater.write((cfg + ui.usr->text().toStdString()).c_str(), 20);

	if (chater.state() == QAbstractSocket::ConnectedState) {
		QMessageBox box;
		box.setText("connected!");
		box.exec();
	}
}
void client_app::out()
{
	chater.disconnectFromHost();
	if (chater.state() != QAbstractSocket::ConnectedState) {
		QMessageBox box;
		box.setText("successfully logout!");
		box.exec();
	}
}
void client_app::post()
{
	if (ui.ip->text().length() == 0 or ui.port->text().length() == 0) {
		QMessageBox box;
		box.setText("please enter your name or ip or port!");
		box.exec();
	}

	QString ip = ui.ip->text();
	qint16 port = ui.port->text().toInt();

	poster.connectToHost(ip, port);
	std::string cfg("<post>");
	if (poster.write((cfg + ui.usr->text().toStdString()).c_str(), 20) != 0)
		poster.write(outcome4posting.c_str(), 40);

	poster.disconnectFromHost();
	if (poster.state() != QAbstractSocket::ConnectedState) {
		QMessageBox box;
		box.setText("your data has been post!");
		box.exec();
	}
}





















